﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class AdvertiesDal : SQLConnection
    {
        public static List<Adverties> Adverties_GetById(string id)
        {
            List<Adverties> list = new List<Adverties>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_GetById", Opencon()))
            {
                Adverties en = new Adverties();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@id", id));
                SqlDataReader reader = dbCmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        list.Add(en.AdvertiesIDataReader(reader));
                    }
                }
                reader.Close();
                //Closecon();
                en = null;
            }
            return list;
        }

        public List<Adverties> Advertise_GetByTop(string top, string where, string order)
        {
            List<Adverties> list = new List<Adverties>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_GetByTop", Opencon()))
            {
                Adverties en = new Adverties();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@top", top));
                dbCmd.Parameters.Add(new SqlParameter("@whrer", where));
                dbCmd.Parameters.Add(new SqlParameter("@order", order));
                SqlDataReader reader = dbCmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        list.Add(en.AdvertiesIDataReader(reader));
                    }
                }
                reader.Close();
                ////Closecon();
                en = null;
            }
            return list;
        }

        public List<Adverties> Advertise_GetByAll(string lang)
        {
            List<Adverties> list = new List<Adverties>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_GetByAll", Opencon()))
            {
                Adverties en = new Adverties();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@lang", lang));
                SqlDataReader reader = dbCmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        list.Add(en.AdvertiesIDataReader(reader));
                    }
                }
                reader.Close();
                ////Closecon();
                en = null;
            }
            return list;
        }

        public List<Adverties> Advertise_Paging(string CurentPage, string PageSize, string Lang)
        {
            List<Adverties> list = new List<Adverties>();
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_Paging", Opencon()))
            {
                Adverties en = new Adverties();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@CurentPage", CurentPage));
                dbCmd.Parameters.Add(new SqlParameter("@PageSize", PageSize));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", Lang));
                SqlDataReader reader = dbCmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        list.Add(en.AdvertiesIDataReader(reader));
                    }
                }
                reader.Close();
                //Closecon();
                en = null;
            }
            return list;
        }

        public bool Advertise_Insert(Adverties data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_Insert", Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.name));
                dbCmd.Parameters.Add(new SqlParameter("@Image", data.image));
                dbCmd.Parameters.Add(new SqlParameter("@Width", data.width));
                dbCmd.Parameters.Add(new SqlParameter("@Height", data.height));
                dbCmd.Parameters.Add(new SqlParameter("@Link", data.link));
                dbCmd.Parameters.Add(new SqlParameter("@Target", data.target));
                dbCmd.Parameters.Add(new SqlParameter("@Content", data.content));
                dbCmd.Parameters.Add(new SqlParameter("@Position", data.position));
                dbCmd.Parameters.Add(new SqlParameter("@PageId", data.pageid == "" || data.pageid == "0" ? DBNull.Value : (object)data.pageid));
                dbCmd.Parameters.Add(new SqlParameter("@Click", data.click));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.lang));
                dbCmd.ExecuteNonQuery();
            }
            System.Web.HttpContext.Current.Cache.Remove("Adverties");
            return true;
        }

        public bool Advertise_Update(Adverties data)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_Update", Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", data.id));
                dbCmd.Parameters.Add(new SqlParameter("@Name", data.name));
                dbCmd.Parameters.Add(new SqlParameter("@Image", data.image));
                dbCmd.Parameters.Add(new SqlParameter("@Width", data.width));
                dbCmd.Parameters.Add(new SqlParameter("@Height", data.height));
                dbCmd.Parameters.Add(new SqlParameter("@Link", data.link));
                dbCmd.Parameters.Add(new SqlParameter("@Target", data.target));
                dbCmd.Parameters.Add(new SqlParameter("@Content", data.content));
                dbCmd.Parameters.Add(new SqlParameter("@Position", data.position));
                dbCmd.Parameters.Add(new SqlParameter("@PageId", data.pageid == "" || data.pageid == "0" ? DBNull.Value : (object)data.pageid));
                dbCmd.Parameters.Add(new SqlParameter("@Click", data.click));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", data.ord));
                dbCmd.Parameters.Add(new SqlParameter("@Active", data.active));
                dbCmd.Parameters.Add(new SqlParameter("@Lang", data.lang));
                dbCmd.ExecuteNonQuery();
            }
            System.Web.HttpContext.Current.Cache.Remove("Adverties");
            return true;
        }

        public bool Advertise_Delete(string Id)
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_Advertise_Delete", Opencon()))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@Id", Id));
                dbCmd.ExecuteNonQuery();

                
            }
            System.Web.HttpContext.Current.Cache.Remove("Advertise");
            return true;
        }

        

    }
}
